import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_learn/main_bloc.dart';
import 'package:flutter_learn/widget_learn/column_page.dart';
import 'package:flutter_learn/widget_learn/did_change_dependence.dart';
import 'package:flutter_learn/widget_learn/element_get_size.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: BlocProvider(
          create: (context) => MainBloc(),
          child: const MyHomePage(title: 'Flutter Demo Home Page'),
        ));
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class Dod {
  late String? name;

  Dod({this.name});
}

class _MyHomePageState extends State<MyHomePage> {
  final int _counter = 0;
  late MainBloc _mainBloc;
  VoidCallback? callback;

  void _incrementCounter() {
    _mainBloc.add(const EatEvent('吃包子'));
    final dog = Dod(name: 'hao');
    print('dog ${dog.name}');
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _mainBloc = BlocProvider.of<MainBloc>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            BlocBuilder<MainBloc, MainState>(
              builder: (context, state) {
                if (state is MainEatState) {
                  return Text('${state.foodName}');
                } else {
                  return Container();
                }
              },
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            OutlinedButton(
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => const ColumnPage()));
              },
              child: const Text('Column点我'),
            ),
            OutlinedButton(
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => const TestDidChangeDependencies2()));
              },
              child: const Text('DidChangeDependence点我'),
            ),
            OutlinedButton(
              onPressed: () {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => const MyGetSizePage()));
              },
              child: const Text('ElementGetSize点我'),
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class NoMethodException implements Exception {
  final String name;
  final String? sex;

  NoMethodException(this.name, this.sex);

  @override
  String toString() {
    return super.toString();
  }
}
